/*
 * @Author: your name
 * @Date: 2021-01-25 20:25:44
 * @LastEditTime: 2021-01-25 21:19:52
 * @LastEditors: Please set LastEditors
 * @Description: 页面内容
 * @FilePath: \ad-api\routes\admin\pageContent.js
 */
const express = require('express');
const router = express.Router();
const Result = require('../../models/result.js');
const { addContentService , getContentService ,delContentService } = require('../../service/contentService')
const upload = require('../../utils/multer')
 //获取页面内容
router.post('/page_content', async (req, res, next)=>{
           const { page_id } = req.body
           const {results ,status} = await getContentService(page_id)
           if(status){
            new Result(results , "查询成功").success(res)
           }else{
            new Result({},'服务器错误,查询失败').fail(res) 
           }
}) 
//添加页面内容
router.post('/add_content',upload.array('image'), async (req, res, next)=>{
    const images = req.files
    let imgsInfo = [] //图片信息数组
    images.forEach((file) => {
        let obj = {}
        obj.name = file.filename
        obj.imgPath = `public/img/${obj.name}`
        obj.path = file.path
        imgsInfo.push(obj)
    })
    const { status } = await addContentService({imgsInfo , ...req.body})
    if (status) {
        new Result({}, '添加成功').success(res)
    } else {
        new Result({}, '添加失败').fail(res)
    }
})

//根据id删除指定页面内容
router.post('/del_content', async (req, res, next) => {
 const { content_id, img_name } = req.body
 const ress = await delContentService(content_id, img_name)
 if (ress) {
     new Result(req.body, '删除成功').success(res)
 } else {
     new Result({}, '删除失败').fail(res)
 }
})
module.exports = router;